package com.zsgeek.wisdchain.qutu.view.me;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zsgeek.wisdchain.qutu.BaseActivity;
import com.zsgeek.wisdchain.qutu.R;
import com.zsgeek.wisdchain.qutu.base.customControl.ColorFlipPagerTitleView;
import com.zsgeek.wisdchain.qutu.base.model.QueryUserModel;
import com.zsgeek.wisdchain.qutu.tools.ImageUtils;
import com.zsgeek.wisdchain.qutu.view.MainActivity;
import com.zsgeek.wisdchain.qutu.view.home.adapter.HomePagerAdapter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 个人主页
 */
public class ViewProfileActivity extends BaseActivity {

    @BindView(R.id.avatar_iv)
    ImageView avatarIv;
    @BindView(R.id.personal_information_tv)
    TextView personalInformationTv;
    @BindView(R.id.nick_name_tv)
    TextView nick_name_tv;
    @BindView(R.id.address_tv)
    TextView address_tv;
    @BindView(R.id.attention_tv)
    TextView attentionTv;
    @BindView(R.id.attention_rl)
    RelativeLayout attentionRl;
    @BindView(R.id.like_tv)
    TextView likeTv;
    @BindView(R.id.like_rl)
    RelativeLayout likeRl;
    @BindView(R.id.comment_or_forward_tv)
    TextView commentOrForwardTv;
    @BindView(R.id.comment_or_forward_rl)
    RelativeLayout commentOrForwardRl;

    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    private List<String> titleList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_view_profile;
    }

    @Override
    protected void initView() {
        setTitle("个人主页");
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        QueryUserModel.DataBean userDataBean = MainActivity.userDataBean;
        attentionTv.setText(String.valueOf(userDataBean.getGuanzhuNum()));
        likeTv.setText(String.valueOf(userDataBean.getDianzhangNum()));
        commentOrForwardTv.setText(String.valueOf(userDataBean.getPinglunNum()));
        QueryUserModel.DataBean.UserBean userBean = userDataBean.getUser();
        if (userBean != null) {
            nick_name_tv.setText(userBean.getNickName());
            address_tv.setText(String.format("地址：%s", userBean.getAddr()));
            ImageUtils.loadImage(ViewProfileActivity.this, userBean.getHeadImg(), avatarIv);

            titleList.add("发布");
            titleList.add("收藏");
            viewpager.setAdapter(new HomePagerAdapter(getSupportFragmentManager(), getFragments(userBean.getAddr())));
            viewpager.setOffscreenPageLimit(1);
            initMagicIndicator();
        }
    }

    @OnClick({R.id.attention_rl, R.id.like_rl, R.id.comment_or_forward_rl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.attention_rl:
                break;
            case R.id.like_rl:
                break;
            case R.id.comment_or_forward_rl:
                break;
        }
    }

    private ArrayList<Fragment> getFragments(String addr) {
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < titleList.size(); i++) {
            fragments.add(new ReleaseOrFavoriteFragment(i, addr));
        }
        return fragments;
    }

    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setScrollPivotX(0.65f);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titleList == null ? 0 : titleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setText(titleList.get(index));
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(ViewProfileActivity.this, R.color.color_333333));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(ViewProfileActivity.this, R.color.color_333333));
                simplePagerTitleView.setTextSize(18);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewpager.setCurrentItem(index, true);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setLineHeight(UIUtil.dip2px(context, 3));
                indicator.setLineWidth(UIUtil.dip2px(context, 14));
//                indicator.setBackgroundResource(R.mipmap.rounded_rectangle_select_blue_bg);
                indicator.setBackgroundColor(getResources().getColor(R.color.transparent));
                indicator.setColors(ContextCompat.getColor(ViewProfileActivity.this, R.color.color_333333));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewpager);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });

        viewpager.setCurrentItem(0);
    }
}
